package com.zlsy.secondtime.threadcoreknowledge.uncaughtexception;

/**
 * @author zhouliang
 * @date 2020/5/19 9:04
 * 1、不加try catch 抛出4个异常，都是带线程名字的。
 * 2、加了try catch 期望捕获到第一个线程的异常，线程234不应该被运行，希望看到打印出caught exception
 * 3、执行时发现，根本没有caught exception，线程234仍然是正常运行
 * <p>
 * 说明线程的异常不能用传统的方法去捕获。
 */
public class CantCatchDirectly implements Runnable {

    @Override
    public void run() {
        throw new RuntimeException();
    }

    public static void main(String[] args) throws InterruptedException {

        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandlerHandler("获取器"));

        new Thread(new CantCatchDirectly(), "myThread-1").start();
        Thread.sleep(300);
        new Thread(new CantCatchDirectly(), "myThread-2").start();
        Thread.sleep(300);
        new Thread(new CantCatchDirectly(), "myThread-3").start();
        Thread.sleep(300);
        new Thread(new CantCatchDirectly(), "myThread-4").start();
    }
}
